package com.jk.kdd.console.service;

import com.jk.kdd.console.dao.SystemVariableClassifyMapper;
import com.jk.kdd.console.dao.SystemVariableMapper;
import com.jk.kdd.console.model.SystemVariable;
import com.jk.kdd.console.model.SystemVariableClassify;
import com.jk.util.BaseDataUtil;
import org.smart4j.framework.util.CastUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 *
 * @author hanping.fu
 * @project jk-console
 * @data 2017/03/07
 * @desc
 */

@Service("systemVariableService")
public class SystemVariableServiceImpl implements  SystemVariableService{

    @Resource
    private SystemVariableMapper systemVariableMapper;



    /**
     * 根据分类类型查询分类下列表
     */
    @Override
    public List<SystemVariable> getClassifyByType(String classifyType) {

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("classifyType", classifyType);
        List<SystemVariable> systemVariableList = systemVariableMapper.selectByCondition(paramMap);

        return systemVariableList;
    }


    /**
     * 查询出最大的var_key
     */
    @Override
    public Long selectMaxVarKey(String classifyType) {

        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("classifyType",classifyType);
        Long maxVarKey = systemVariableMapper.selectMaxVarKey(paramMap);
        return maxVarKey;
    }


    /**
     * 创建分类
     */
    @Override
    @Transactional
    public boolean createClassify(String varType, String varName,String operateId) {
        Map<String,Object> paramMap1 = new HashMap<>();
        paramMap1.put("varName",varName);
        //通过varName查询是否存在记录
        SystemVariable svc = systemVariableMapper.selectByVarName(paramMap1);

        if(svc!=null){
            return false;
        }
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("varType",varType);
        //查询出最大的var_key
        Long varKey = systemVariableMapper.selectMaxVarKey(paramMap);
        Long varKeyNext = varKey + 1;
        SystemVariable systemVariable = new SystemVariable();
        systemVariable.setVarType(varType);
        systemVariable.setVarKey(varKeyNext);
        systemVariable.setVarName(varName);
        systemVariable.setSerialNumber(Integer.parseInt(varKeyNext.toString()));
        BaseDataUtil.setBaseDataForEntity(systemVariable, "1", operateId);
        systemVariableMapper.insertSelective(systemVariable);

        return true;
    }

    /**
     * 修改分类
     */
    @Override
    @Transactional
    public boolean modifyClassify(String varType,String varKey,String varName, String operateId) {

        SystemVariable systemVariable = new SystemVariable();
        systemVariable.setVarType(varType);
        systemVariable.setVarKey(CastUtil.castLong(varKey));
        systemVariable.setVarName(varName);
        systemVariable.setSerialNumber(Integer.parseInt(varKey.toString()));

        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("varType",systemVariable.getVarType());
        paramMap.put("varKey",systemVariable.getVarKey());

        // 设置基础数据
        SystemVariable tempSystemVariable = systemVariableMapper.selectByTypeAndKey(paramMap);
        systemVariable.setUpdateCount(tempSystemVariable.getUpdateCount());
        // 更新用户
        BaseDataUtil.setBaseDataForEntity(systemVariable,"2",operateId);
        systemVariableMapper.updateByTypeAndKey(systemVariable);

        return true;
    }

    /**
     * 删除分类
     */
    @Override
    @Transactional
    public boolean deleteClassify(String varType,String varKey, String operateId) {

        SystemVariable systemVariable = new SystemVariable();
        systemVariable.setVarType(varType);
        systemVariable.setVarKey(CastUtil.castLong(varKey));
        systemVariable.setSerialNumber(Integer.parseInt(varKey.toString()));
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("varType",systemVariable.getVarType());
        paramMap.put("varKey",systemVariable.getVarKey());
        // 设置基础数据
        systemVariable = systemVariableMapper.selectByTypeAndKey(paramMap);
        BaseDataUtil.setBaseDataForEntity(systemVariable,"3",operateId);
        systemVariableMapper.updateByTypeAndKey(systemVariable);

        return true;
    }

    /**
     * 分类列表排序
     */
    @Override
    @Transactional
    public boolean sortVariableList(String varType1, String varKey1, String varType2, String varKey2, String sNum1, String sNum2,String operateId) {

        Map<String,Object> paramMap1 = new HashMap<>();
        paramMap1.put("varType",varType1);
        paramMap1.put("varKey",varKey1);
        // 设置基础数据
        SystemVariable systemVariable1 = systemVariableMapper.selectByTypeAndKey(paramMap1);
        systemVariable1.setSerialNumber(Integer.parseInt(sNum2));
        BaseDataUtil.setBaseDataForEntity(systemVariable1,"2",operateId);
        systemVariableMapper.updateByTypeAndKey(systemVariable1);

        Map<String,Object> paramMap2 = new HashMap<>();
        paramMap2.put("varType",varType2);
        paramMap2.put("varKey",varKey2);
        // 设置基础数据
        SystemVariable systemVariable2 = systemVariableMapper.selectByTypeAndKey(paramMap2);
        systemVariable2.setSerialNumber(Integer.parseInt(sNum1));
        BaseDataUtil.setBaseDataForEntity(systemVariable2,"2",operateId);
        systemVariableMapper.updateByTypeAndKey(systemVariable2);

        return true;
    }


}
